package com.example.service.implService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.config.ResponseVO;
import com.example.mapper.ApprovalMapper;
import com.example.mapper.LeaveRequestMapper;
import com.example.pojo.Approval;
import com.example.pojo.LeaveRequest;
import com.example.pojo.dto.LeaveRequestDTO;
import com.example.service.LeaveRequestService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class LeaveRequestServiceImpl implements LeaveRequestService {
    @Resource
    private LeaveRequestMapper leaveRequestMapper;
    @Resource
    private ApprovalMapper approvalMapper;

    // 根据班级id查询出该班级还没有批准的请假
    @Override
    public ResponseEntity<ResponseVO<List<LeaveRequest>>> leaveRequestListByClassId(Long classId) {
        QueryWrapper<LeaveRequest> leaveRequestQueryWrapper = new QueryWrapper<>();
        leaveRequestQueryWrapper.eq("class_id", classId);
        leaveRequestQueryWrapper.eq("leave_status", 1);
        List<LeaveRequest> leaveRequests = leaveRequestMapper.selectList(leaveRequestQueryWrapper);
        if (!Objects.isNull(leaveRequests)) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "获取成功", leaveRequests));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "获取失败"));
    }

    // 根据班级id查询出该班级还没有批准的请假LeaveRequestDTO
    @Override
    public ResponseEntity<ResponseVO<List<LeaveRequestDTO>>> getNotProcessedLeave(Long classId) {
        List<LeaveRequestDTO> leaveRequestsByClassId = leaveRequestMapper.findLeaveRequestsByClassId(classId);
        if (!Objects.isNull(leaveRequestsByClassId)) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "获取成功", leaveRequestsByClassId));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "获取失败"));
    }

    @Override
    public ResponseEntity<ResponseVO<List<LeaveRequestDTO>>> getProcessedLeave(Long classId) {
        List<LeaveRequestDTO> leaveRequestsByClassId = leaveRequestMapper.getProcessedLeave(classId);
        if (!Objects.isNull(leaveRequestsByClassId)) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "获取成功", leaveRequestsByClassId));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "获取失败"));
    }

    // 根据学生id查询该学生是否还有未批准的假期
    @Override
    public ResponseEntity<ResponseVO<LeaveRequest>> leaveRequestByStuId(Long stuId) {
        QueryWrapper<LeaveRequest> leaveRequestQueryWrapper = new QueryWrapper<>();
        leaveRequestQueryWrapper.eq("student_id", stuId);
        LeaveRequest leaveRequest = leaveRequestMapper.selectOne(leaveRequestQueryWrapper);
        if (!Objects.isNull(leaveRequest)) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "获取成功", leaveRequest));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "还没有未批准的假期"));
    }

    @Override
    public ResponseEntity<ResponseVO<LeaveRequest>> addLeaveRequest(LeaveRequestDTO leaveRequestDTO) {
        // 创建一个请假对象
        LeaveRequest leaveRequest = getLeaveRequest(leaveRequestDTO);
        // 添加到数据库中
        leaveRequestMapper.insert(leaveRequest);
        // 根据学生id查询出刚刚添加的那一条请假的数据
        QueryWrapper<LeaveRequest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", leaveRequestDTO.getStudentId());
        LeaveRequest leaveRequestByStuId = leaveRequestMapper.selectOne(queryWrapper);
        // 然后添加到请假信息关联表中
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "休假成功", leaveRequestByStuId));
    }

    @Override
    public ResponseEntity<ResponseVO<String>> processedLeave(String remark, Long teacherId, LeaveRequestDTO leaveRequestDTO) {
        // 首先修改当前的假期
        LeaveRequest leaveRequest = getLeaveRequest(leaveRequestDTO);
        leaveRequest.setAuditDate(LocalDateTime.now());
        leaveRequestMapper.update(leaveRequest,null);
        Approval approval = new Approval();
        System.out.println(leaveRequest);
        approval.setLeaveId(leaveRequest.getLeaveId());
        approval.setComments(remark);
        approval.setTeacherId(teacherId);
        System.out.println(approval);
        int insert = approvalMapper.insert(approval);
        if(insert>0){
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "批准成功"));
        }
        // 添加假期批准记录
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "批准失败"));
    }

    private static LeaveRequest getLeaveRequest(LeaveRequestDTO leaveRequestDTO) {
        LeaveRequest leaveRequest = new LeaveRequest();
        leaveRequest.setLeaveId(leaveRequestDTO.getLeaveId());
        leaveRequest.setStudentId(leaveRequestDTO.getStudentId());
        leaveRequest.setLeaveType(leaveRequestDTO.getLeaveType());
        leaveRequest.setClassId(leaveRequestDTO.getClassId());
        leaveRequest.setStartDate(leaveRequestDTO.getStartDate());
        leaveRequest.setLeaveStatus(leaveRequestDTO.getLeaveStatus());
        leaveRequest.setEndDate(leaveRequestDTO.getEndDate());
        leaveRequest.setLeaveDays(leaveRequestDTO.getLeaveDays());
        leaveRequest.setReason(leaveRequestDTO.getReason());
        leaveRequest.setImageUrls(leaveRequestDTO.getImageUrls());
        leaveRequest.setPublishDate(LocalDateTime.now());
        return leaveRequest;
    }
}
